package com.example.mymusic.utils;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.example.mymusic.app.MyApp;

import java.lang.reflect.Method;
import java.util.Set;

public class BluetoothController {
    private static final String TAG="蓝牙";

    public static BluetoothController getInstance() {
        return SingleInstance.sInstance;
    }

    /**
     * 静态内部类，单例
     */
    private static class SingleInstance {
        private static final BluetoothController sInstance = new BluetoothController();
    }

    public static BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//蓝牙控制

    //蓝牙设备

    public static Boolean openBluetooth(Activity activity) {
        if (bluetoothAdapter == null) {
            // 设备不支持蓝牙
        } else {
            checkPermission(activity);
            if (!bluetoothAdapter.isEnabled()) {
                return bluetoothAdapter.enable();//打开蓝牙
            } else {
                return true;
            }
        }
        return false;
    }

    public static Boolean closeBluetooth(Activity activity) {
        if (bluetoothAdapter == null) {
            // 设备不支持蓝牙
        } else {
            checkPermission(activity);
            if (bluetoothAdapter.isEnabled()) {
                return bluetoothAdapter.disable();//关闭蓝牙
            } else {
                return true;
            }
        }
        return false;
    }

    //权限
    public static String[] PERMISSIONS = {
            "android.permission.BLUETOOTH",//蓝牙
            "android.permission.BLUETOOTH_ADMIN",//蓝牙管理
            "android.permission.ACCESS_FINE_LOCATION",//访问精细定位
            "android.permission.ACCESS_COARSE_LOCATION", //大致定位
            "android.permission.BLUETOOTH_CONNECT", //蓝牙连接
            "android.permission.BLUETOOTH_SCAN"
    };
    public static final int REQUEST_CODE = 1002;

    /**
     * 根据判断是否开启相关蓝牙权限，没开启显示授权
     *
     * @param activity
     */
    public static void checkPermission(Activity activity) {
        for (String permission : PERMISSIONS) {//PackageManager.PERMISSION_GRANTED 已授权
            if (ActivityCompat.checkSelfPermission(MyApp.getInstance(), permission) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MyApp.getInstance(), "请开通相关权限！", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(activity, PERMISSIONS, REQUEST_CODE);
                break;
            }
        }
    }

    /**
     * 利用反射获取当前连接的蓝牙信息
     */
    public static void getBluetoothDevice() {
        Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
        try {//得到蓝牙状态的方法
            Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
            //打开权限
            method.setAccessible(true);
            int state = (int) method.invoke(bluetoothAdapter, (Object[]) null);

            Log.d(TAG, "getBluetoothDevice state: "+state);

            if (state == BluetoothAdapter.STATE_CONNECTED) {

                Log.i(TAG, "BluetoothAdapter.STATE_CONNECTED");

                if (ActivityCompat.checkSelfPermission(MyApp.getInstance(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
                Log.i(TAG, "devices:" + devices.size());

                for (BluetoothDevice device : devices) {

                    Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
                    method.setAccessible(true);
                    boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);

                    if (isConnected) {
                        Log.i(TAG, "Address:" + device.getAddress());
                        Log.d(TAG, "name: "+device.getName());
                        Log.d(TAG, "type: "+device.getType());
                        Log.d(TAG, "uuid: "+ device.getUuids()[0]);
                        Log.d(TAG, "getBondState: "+device.getBondState());
//                        mBluetoothDevice = device;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}